package com.example.day0818_sever;

import com.example.day0818_sever.slice.MainAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.window.dialog.ToastDialog;
import ohos.bundle.IBundleManager;
import ohos.security.SystemPermission;

public class MainAbility extends Ability {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(MainAbilitySlice.class.getName());
        ApplyForPermission();
    }
    // 申请权限
    private void ApplyForPermission(){
        String[] permission = new String[]{SystemPermission.DISTRIBUTED_DATASYNC};
        if (verifySelfPermission(SystemPermission.DISTRIBUTED_DATASYNC) != IBundleManager.PERMISSION_GRANTED) {
            requestPermissionsFromUser(permission,1000);
        }
    }
    // 权限回调

    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        if (permissions.length == 0 || permissions == null|| grantResults.length==0 || grantResults==null) {
            super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
            return;
        }
        if (requestCode == 1000) {
            if (grantResults[0] == IBundleManager.PERMISSION_DENIED) {
                terminateAbility();
            }
            if (grantResults[0] == IBundleManager.PERMISSION_GRANTED) {
                new ToastDialog(this).setText("已授权").show();
            }
        }
    }
}
